{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple CPW Meander\n",
    "\n",
    "We'll be creating a 2D design and adding a meandered resonator QComponent.  \n",
    "Will use component called OpenToGround for termination of resonators.\n",
    "\n",
    "Simple RouteMeander resonator object will be shown. \n",
    "\n",
    "RouteMeander: Implements a simple CPW, with a single meander.  \n",
    "OpenToGround: A basic open to ground termination. Functions as a pin for auto drawing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For convenience, let's begin by enabling\n",
    "# automatic reloading of modules when they change.\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import qiskit_metal as metal\n",
    "from qiskit_metal import designs, draw\n",
    "from qiskit_metal import MetalGUI, Dict, Headings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Each time you create a new quantum circuit design, \n",
    "# you start by instantiating a QDesign class. \n",
    "\n",
    "# The design class `DesignPlanar` is best for 2D circuit designs.\n",
    "design = designs.DesignPlanar()\n",
    "gui = MetalGUI(design)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from qiskit_metal.qlibrary.terminations.open_to_ground import OpenToGround\n",
    "from qiskit_metal.qlibrary.tlines.meandered import RouteMeander"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Be aware of the default_options that can be overridden by user.\n",
    "RouteMeander.get_template_options(design)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Be aware of the default_options that can be overridden by user.\n",
    "OpenToGround.get_template_options(design)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# To force overwrite a QComponent with an existing name.  \n",
    "# This is useful when re-running cells in a notebook. \n",
    "design.overwrite_enabled = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A RouteMeander connector are shown.  The terminations are open-to-ground. \n",
    "\n",
    "The pin_inputs is the default dictionary for passing pins into a component, **BUT** how the dictionary is structured is component dependent. Using the below structure (eg. start_pin, end_pin) is suggested for any 2 port type connection, but you should always check the documentation for the specific component you are wanting to use."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "open_start_options = Dict(pos_x='1000um',\n",
    "                         pos_y='0um',\n",
    "                         orientation = '-90')\n",
    "\n",
    "open_start_meander = OpenToGround(design,'Open_meander_start',options=open_start_options)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "open_end_options = Dict(pos_x='1000um',\n",
    "                        pos_y='1500um',\n",
    "                        orientation='90',\n",
    "                        termination_gap='10um')\n",
    "\n",
    "open_end_meander = OpenToGround(design,'Open_meander_end',options=open_end_options)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "meander_options = Dict(pin_inputs=Dict(start_pin=Dict(\n",
    "                                            component='Open_meander_start',\n",
    "                                            pin='open'),\n",
    "                                        end_pin=Dict(\n",
    "                                            component='Open_meander_end',\n",
    "                                            pin='open')\n",
    "                                        ),\n",
    "                      total_length='9mm',\n",
    "                      fillet='99.99um')\n",
    "\n",
    "testMeander = RouteMeander(design,'meanderTest',options=meander_options)\n",
    "gui.rebuild()\n",
    "gui.autoscale()\n",
    "gui.zoom_on_components([testMeander.name, open_start_meander.name, open_end_meander.name]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Let's see what the testMeander object looks like\n",
    "testMeander #print meanderTest information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Let's see what the open_start_meander object looks like\n",
    "open_start_meander #print Open_meander_start information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also see what active connections there are from the netlist. Pins that share the same net_id indicate they are connected. Pins that are not on the net list are currently open."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "design.net_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save screenshot as a .png formatted file.\n",
    "gui.screenshot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "nbsphinx-thumbnail"
    ]
   },
   "outputs": [],
   "source": [
    "# Screenshot the canvas only as a .png formatted file.\n",
    "gui.figure.savefig('shot.png')\n",
    "\n",
    "from IPython.display import Image, display\n",
    "_disp_ops = dict(width=500)\n",
    "display(Image('shot.png', **_disp_ops))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Closing the Qiskit Metal GUI\n",
    "gui.main_window.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
